﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.Data.Providers.SqlServer.Query;

namespace NBusiness.Query.Testing
{
    [TestClass]
    class InsertTests
    {
        [TestMethod]
        public void SimpleInsert()
        {
            IInsert insert = new SqlInsert().Into(new SqlTable("Example"), new SqlColumn("Text")).Values(new SqlParameter("Text"));

            string expected = "INSERT INTO [Example] ([Text]) VALUES (@Text)";
            string actual = insert.ToSql();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void MultiValueInsert()
        {
            ITable table = new SqlTable("T");
            IColumn c1 = new SqlColumn("c1", table);
            IColumn c2 = new SqlColumn("c2", table);
            IParameter p1 = new SqlParameter("p1");
            IParameter p2 = new SqlParameter("p2");

            IQuery query = new SqlQuery();
            query.Insert().Into(table, c1, c2).Values(p1, p2);

            string expected = "INSERT INTO [T] ([c1], [c2]) VALUES (@p1, @p2)";
            string actual = query.ToSql();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullTableFail()
        {
            new SqlQuery().Insert().Into(null);
        }

        [TestMethod]
        public void TestParameters1()
        {
            IInsert i = new SqlInsert();
            IParameterValue p1 = i.Parameter("p1");
            IParameterValue p2 = i.Parameter("p2");

            Assert.AreEqual(2, i.Parameters.Count);
            Assert.AreSame(p1, i.Parameters[0]);
            Assert.AreSame(p2, i.Parameters[1]);
        }

        [TestMethod]
        public void TestUniqueParameters()
        {
            IInsert i = new SqlInsert();
            IParameterValue p1 = i.Parameter("p1");
            IParameterValue p2 = i.Parameter("p1");

            Assert.AreSame(p1, p2);
        }

        [TestMethod]
        public void TestParameterWithValue()
        {
            IInsert i = new SqlInsert();
            i.Parameter("p1", ParameterValueDirection.Output).Value = true;

            Assert.AreEqual(1, i.Parameters.Count);
            Assert.AreEqual("p1", i.Parameters[0].Name);
            Assert.AreEqual(ParameterValueDirection.Output, i.Parameters[0].Direction);
            Assert.AreEqual(true, i.Parameters[0].Value);
        }

    }
}
